package com.cadre.system.olddomain;

import cn.hutool.extra.spring.SpringUtil;
import com.cadre.system.mapper.DIC_POSITIONMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ClassUtils;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Segment {
    private static final int TYPE_POST = 0;
    private static final int TYPE_UNIT = 1;
    private Set<String> seg_dict;
    @Resource
    private DIC_POSITIONMapper dIC_POSITIONMapper = (DIC_POSITIONMapper) SpringUtil.getApplicationContext().getBean(DIC_POSITIONMapper.class);

    public void setDic(String dicName) {
        this.seg_dict = new HashSet();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(ClassUtils.getDefaultClassLoader().getResource("").getPath() + "/" + dicName)));
            while (true) {
                String line = br.readLine();
                if (line != null) {
                    String line2 = line.trim();
                    if (!line2.isEmpty()) {
                        this.seg_dict.add(line2);
                    }
                } else {
                    br.close();
                    return;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Segment(int type) {
        this.seg_dict = new HashSet();
        if (type == 0) {
            this.seg_dict = new HashSet();
            this.seg_dict = this.dIC_POSITIONMapper.selectAll();
        }
        if (type == 1) {
            this.seg_dict = new HashSet();
            this.seg_dict = this.dIC_POSITIONMapper.selectUnit();
        }
    }

    private List<String> FMM2(String phrase) {
        List<String> fmm_list = new ArrayList<>();
        int len_phrase = phrase.length();
        int i = TYPE_POST;
        while (i < len_phrase) {
            int end = i + 21;
            if (end >= len_phrase) {
                end = len_phrase;
            }
            String phrase_sub = phrase.substring(i, end);
            int j = phrase_sub.length();
            while (true) {
                if (j < 0 || j == 1) {
                    break;
                }
                String key = phrase_sub.substring(TYPE_POST, j);
                if (this.seg_dict.contains(key)) {
                    fmm_list.add(key);
                    i += key.length() - 1;
                    break;
                }
                j--;
            }
            if (j == 1) {
                fmm_list.add("" + phrase_sub.charAt(TYPE_POST));
            }
            i++;
        }
        return fmm_list;
    }

    private List<String> BMM2(String phrase) {
        List<String> bmm_list = new ArrayList<>();
        int i = phrase.length();
        while (i > 0) {
            int start = i - 21;
            if (start < 0) {
                start = TYPE_POST;
            }
            String phrase_sub = phrase.substring(start, i);
            int j = TYPE_POST;
            while (true) {
                if (j >= phrase_sub.length() || j == phrase_sub.length() - 1) {
                    break;
                }
                String key = phrase_sub.substring(j);
                if (this.seg_dict.contains(key)) {
                    bmm_list.add(TYPE_POST, key);
                    i -= key.length() - 1;
                    break;
                }
                j++;
            }
            if (j == phrase_sub.length() - 1) {
                bmm_list.add(TYPE_POST, "" + phrase_sub.charAt(j));
            }
            i--;
        }
        return bmm_list;
    }

    public List<String> segment(String phrase) {
        List<String> fmm_list = FMM2(phrase);
        List<String> bmm_list = BMM2(phrase);
        if (fmm_list.size() == bmm_list.size()) {
            int FSingle = TYPE_POST;
            int BSingle = TYPE_POST;
            boolean isSame = true;
            for (int i = TYPE_POST; i < fmm_list.size(); i++) {
                if (!fmm_list.get(i).equals(bmm_list.get(i))) {
                    isSame = false;
                }
                if (fmm_list.get(i).length() == 1) {
                    FSingle++;
                }
                if (bmm_list.get(i).length() == 1) {
                    BSingle++;
                }
            }
            if (isSame) {
                return fmm_list;
            }
            if (BSingle > FSingle) {
                return fmm_list;
            }
            return bmm_list;
        } else if (fmm_list.size() > bmm_list.size()) {
            return bmm_list;
        } else {
            return fmm_list;
        }
    }

    public String[] segmentPostUnit(String str) {
        if (str.equals("") || str == null) {
            return new String[]{"", ""};
        }
        List<String> list = segment(str);
        return new String[]{StringUtils.join(list.subList(TYPE_POST, list.size() - 1), ""), list.get(list.size() - 1)};
    }

    public String[] segmentUnit(String str) {
        if (str.equals("") || str == null) {
            return new String[]{"", ""};
        }
        List<String> list = segment(str);
        List<String> sublist = list.subList(1, list.size());
        String unit = list.get(TYPE_POST);
        String childunit = StringUtils.join(sublist, "");
        if (unit.length() == 1) {
            unit = str;
            childunit = "";
        }
        return new String[]{unit, childunit};
    }
}
